<template>
  <!-- app-container  内置class 周边带间隔 -->
  <div class="app-container">
    <el-form :inline="true" :model="query" class="demo-form-inline">
      <el-form-item label="审批人">
        <el-input v-model="query.user" placeholder="审批人"></el-input>
      </el-form-item>
      <el-form-item label="活动区域">
        <el-select v-model="query.state" clearable filterable placeholder="状态" style="width:85px">
          <el-option
            :label="item.name"
            :value="item.code"
            v-for="item in stateOptions"
            :key="item.code"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="queryData">查询</el-button>
        <el-button type="info" @click="reload">重置</el-button>
        <el-button type="primary" @click="openAdd">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- stripe 带斑马纹的表格 border边框-->
    <el-table :data="list" style="width: 100%" stripe border>
      <el-table-column align="center" type="index" label="序号" width="60"></el-table-column>
      <el-table-column align="center" prop="name" label="分类名称"></el-table-column>
      <el-table-column align="center" prop="name" label="姓名"></el-table-column>
      <el-table-column align="center" prop="name" label="备注"></el-table-column>
      <el-table-column align="center" prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status |statusFilter">{{scope.row.status=='1'?'正常':'禁止'}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button size="mini" @click="handleDelete(scope.row.id)" type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <!--current-page当前页数
    page-sizes 选项每页条数
    page-size 每页多少条
    -->
    <el-pagination
      center
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pages.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pages.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pages.total"
    ></el-pagination>
    <!--方式1 @remoteClose="remoteClose" 配合 this.$emit('remoteClose') -->
    <!--方式2 remoteClose给子组件传一个方法 让子组件调用这个方法  -->
    <edit
      :title="edit.title"
      :visible="edit.visible"
      :formData="edit.formData"
      :remoteClose="remoteClose"
    ></edit>
  </div>
</template>
 
<script>
import category from "@/api/category";
import edit from "./edit";

const stateOptions = [
  { code: 1, name: "正常" },
  { code: 0, name: "禁止" }
];
export default {
  name: "Category",
  components: { edit },
  data() {
    return {
      list: [], //查询数据
      query: {}, //state: "", user: ""
      stateOptions, //下拉框的数据
      dialogVisible: false,
      pages: {
        current: 1, //页数
        size: 20, //每一页的条数
        total: 0 //总的数量
      },
      edit: {
        visible:false,
        title: "",
        formData: {
          name: "",
          status: 1,
          remark: ""
        }
      }
    };
  },
  created() {
    this.fetchData();//查询和获取首次数据是一个接口 查询的时候带上query 
  },
  filters: {
    statusFilter(value) {
      // 0禁用  1正常
      const stateMap = { 0: "danger", 1: "success" };
      return stateMap[value];
      // 方式2
      // if (!value) {
      //   return "danger";
      // } else {
      //   return "success";
      // }
    }
  },
  methods: {
    fetchData() {
      category
        .getList(this.query, this.pages.current, this.pages.size)
        .then(res => {
          this.pages.total = res.data.total;
          this.list = res.data.records; 
          console.log(res);
        });
    },
    //处理页码大小 每页多少条
    handleSizeChange(val) {
      console.log(val, "edwwwwwwwwwwww");
      // 赋值最新的每页条数
      this.pages.size = val;
      // 重新查询
      this.fetchData();
    },
    // 处理前往多少页数
    handleCurrentChange(val) {
      console.log(val, "1111111111111111111");

      this.pages.current = val;
      this.fetchData();
    },
    onSubmit() {
      console.log("submit!");
    },
    // 查询
    queryData() {
      this.pages.current = 1;
      this.fetchData();
    },
    // 重置
    reload() {
      (this.query = {}), this.fetchData();
    },
    // 点击新增按钮打开新增弹出框
    openAdd() {
      this.edit.visible = true;
      this.edit.title = "新增";
    },
    // 子组件触发进行关闭窗口
    remoteClose(val) {
      this.edit.formData = {};
      // 关闭弹窗
      this.edit.visible = false;
      if (val) {
        //0取消不要刷新数据  1取反
        this.fetchData();
      } else {
        return;
      }
    },
    // 编辑数据
    handleEdit(obj) {
      // 方式1
      //obj=scope.row的时候 是吧点击的数据传到子组件的展示页
      // console.log(obj, "编辑");
      // this.edit.visible = true;
      // this.edit.title = "编辑";
      // this.edit.formData = obj;
      // 方式2
      //obj=scope.row.id的时候 是吧点击的数据id传到子组件进行调用接口获取数据在展示
      category.getById(obj).then(res => {
        console.log(res, "编辑");
        if (res.code == 20000) {
          this.edit.title = "编辑";
          this.edit.formData = res.data;
          this.edit.visible = true;
        }
      });
    },
    // 删除数据
    handleDelete(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          category.delete(id).then(res => {
            console.log(res, "delete");
            this.$message({
              type: (res.code = 20000 ? "success" : "error"),
              message: res.message
            });
            this.fetchData();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>